package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.UmsAdminMapper;
import com.mail.model.db.UmsAdmin;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 吕永乐
 * @date 2022/06/21 10:24
 **/
@DubboService
public class UmsAdminApiImpl implements UmsAdminApi{

	@Autowired
	private UmsAdminMapper umsAdminMapper;

	/**
	 * 根据用户名查询用户
	 *
	 * @param username
	 * @return
	 */
	@Override
	public UmsAdmin getAdminByUsername(String username) {
		LambdaQueryWrapper<UmsAdmin> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(StringUtils.isNotEmpty(username), UmsAdmin::getUsername, username);
		return umsAdminMapper.selectOne(wrapper);
	}

	/**
	 * 根据id查询admin
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public UmsAdmin getAdminById(Long userId) {
		return umsAdminMapper.selectById(userId);
	}

	/**
	 * 根据用户名或姓名分页获取用户列表 (分页查询)
	 *
	 * @param pageNum
	 * @param pageSize
	 * @param keyword
	 * @return
	 */
	@Override
	public Page<UmsAdmin> getAdminByPage(Long pageNum, Long pageSize, String keyword) {
		Page<UmsAdmin> page = new Page<>(pageNum, pageSize);
		LambdaQueryWrapper<UmsAdmin> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StringUtils.isNotEmpty(keyword), UmsAdmin::getUsername, keyword)
				.or()
				.like(StringUtils.isNotEmpty(keyword),UmsAdmin::getNickName, keyword);
		return umsAdminMapper.selectPage(page, wrapper);
	}

	/**
	 * 保存数据
	 *
	 * @param umsAdmin
	 * @return
	 */
	@Override
	public void save(UmsAdmin umsAdmin) {
		umsAdminMapper.insert(umsAdmin);
	}

	/**
	 * 根据 id 修改状态
	 *
	 * @param id
	 * @param status
	 */
	@Override
	public void updateStatusById(Long id, Integer status) {
		umsAdminMapper.updateStatusById(id, status);
	}

	/**
	 * 修改指定用户信息
	 *
	 * @param umsAdmin
	 */
	@Override
	public void update(UmsAdmin umsAdmin) {
		umsAdminMapper.updateById(umsAdmin);
	}

	/**
	 * 更新最后登录时间
	 *
	 * @param admin
	 */
	@Override
	public void updateLastLoginTime(UmsAdmin admin) {
		umsAdminMapper.updateById(admin);
	}

	/**
	 * 删除当前用户
	 *
	 * @param adminId
	 */
	@Override
	public void remove(Long adminId) {
		umsAdminMapper.deleteById(adminId);
	}

	/**
	 * 根据用户名修改密码
	 *
	 * @param umsAdmin
	 */
	@Override
	public void updatePasswordByUsername(UmsAdmin umsAdmin) {
		umsAdminMapper.updatePasswordByUsername(umsAdmin.getUsername(), umsAdmin.getPassword());
	}
}
